// pages/projectcreat/projectcreat.ts
import {createStoreBindings} from "mobx-miniprogram-bindings"
import {store} from "../../store/store"
import axios from "../../utils/axios"

Page({

  /**
   * 页面的初始数据
   */
  data: {
  date:new Date().getFullYear()+"-"+new Date().getMonth()+"-"+new Date().getDay(),
    tagkey:"互联网+",
    studylevel:["请选择","小学","初中","高中","大学","专科","硕士","博士"],
    index:0,
    number:0,
    number1:0,
    name:0,
    hoster:'',
    T:"",
    place:"",
    experience:"",
    level:"",
    wantperson:"",
    introduction:"",
    id:0,
    fail:false
  },
  datechage:function(e){
    console.log(666)
    this.setData({
      date:e.detail.value
    })
  },
  gethoster:function(e){
    this.setData({
      hoster:e.detail.value
    })
  },
  getT:function(e){
    this.setData({
      T:e.detail.value
    })
  },
  getplace:function(e){
    this.setData({
      place:e.detail.value
    })
  },
  getintroduction:function(e){
    this.setData({
      introduction:e.detail.value
    })
  },
  getexperience:function(e){
    this.setData({
      experience:e.detail.value
    })
  },
  getwantperson:function(e){
    this.setData({
      wantperson:e.detail.value
    })
  },
  getlevel:function(e){
    this.setData({
      level:e.detail.value
    })
  },
  add:function(){
    this.setData({
      number:this.data.number+1
    })
  },
  sub:function(){
    this.setData({
      number:(this.data.number==0)?this.data.number:this.data.number-1
    })
  },
  addtag:function(){
    var tags=this.data.tagkey
    if(tags.length<2){
      tags.push("")
    }
    this.setData({
      tagkey:tags
    })
  },
  gettag:function(e){
    this.setData({
      tagkey:e.detail.value
    })
  },
  subtag:function(e){
    var tags=this.data.tagkey
     tags.splice(e.currentTarget.dataset.index,1)
     this.setData({
       tagkey:tags
     })
  },
  add1:function(){
    this.setData({
      number1:this.data.number1+1
    })
  },
  sub1:function(){
    this.setData({
      number1:(this.data.number1==0)?this.data.number1:this.data.number1-1
    })
  },
  numberchange1:function(e){
    this.setData({
      number1:parseInt(e.detail.value)
    })
  },
  numberchange:function(e){
    this.setData({
      number:parseInt(e.detail.value)
    })
  },
  studylevel:function(e){
    this.setData({
      index:e.detail.value
    })
  },
  getname:function(e){
    this.setData({
      name:e.detail.value
    })
  },
 async projectsumbit(){
   console.log(this.data)
   var that=this
    wx.showModal({
      title:"确定提交项目吗",
      success:async function(res){
        if(res.confirm){
          var token=getApp(). globalData.accessToken
          var data={
            name:that.data.name,
        tag:that.data.tagkey,
        organizer:that.data.hoster,
        location:that.data.place,
        introduction:that.data.introduction,
        rop:that.data.level,
        period:that.data.T,
        count:that.data.number1,
        scope:that.data.number,
        expect:that.data.wantperson,
        id:that.data.id
          }
        var result= await axios.patch('/project',data)
    console.log(result) 
          await wx.p.showToast({
              title:"提交成功"
            })
           await wx.p.navigateBack({
              delta:1
            })
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {  
    var fail=JSON.parse(option.fail)
        createStoreBindings(this,{
          store,
          fields:["project"],
          actions:['getproject']
        })
      var project=store.getproject
      var datetime=new Date(project.end)
      var date=datetime.getFullYear()+"-"+datetime.getMonth()+"-"+datetime.getDate()
      this.setData({
        date:date,
        tagkey:project.tag,
        number:project.scope,
        number1:project.count,
        name:project.name,
        hoster:project.organizer,
        T:project.period,
        place:project.location,
        level:project.rop,
        wantperson:project.expect,
        introduction:project.introduction,
        id:project.id,
        fail:fail
      })
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})